package com.navinfo.app.hd;

import static org.oscim.android.canvas.AndroidGraphics.drawableToBitmap;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;

import org.oscim.android.canvas.AndroidGraphics;
import org.oscim.backend.CanvasAdapter;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.backend.canvas.Color;
import org.oscim.layers.marker.MarkerSymbol;
import org.oscim.layers.vector.geometries.Style;
import org.oscim.renderer.bucket.TextureItem;
import org.oscim.theme.styles.LineStyle;

public class LayerStyle {
    private static MarkerSymbol defaultMarkerSymbol;
    private static Style defaultPointStyle;
    private static Style defaultLineStyle;
    private static Style defaultPolygonStyle;
    private static MarkerSymbol geoJsonMarkerSymbol;
    private static LineStyle naviPathLineStyle;
    private static final int DEFAULT_LINE_WIDTH= /*(int)CanvasAdapter.getScale()**/5;
    private static final int DRAW_LINE_COLOR = Color.RED;

    public static MarkerSymbol getDefaultMarkerSymbol(Context mContext) {
        if (defaultMarkerSymbol == null) {
            Bitmap bitmapPoi = drawableToBitmap(mContext.getResources().getDrawable(R.drawable.marker_focus));
            defaultMarkerSymbol = new MarkerSymbol(bitmapPoi, MarkerSymbol.HotspotPlace.CENTER);
        }
        return defaultMarkerSymbol;
    }

    public static Style getDefaultPointStyle() {
        if (defaultPointStyle == null) {
            defaultPointStyle = Style.builder()
                    .stippleColor(DRAW_LINE_COLOR)
                    .stipple(DEFAULT_LINE_WIDTH)
                    .stippleWidth(DEFAULT_LINE_WIDTH)
                    .strokeWidth(DEFAULT_LINE_WIDTH)
                    .strokeColor(DRAW_LINE_COLOR)
                    .buffer(2)
                    .scaleZoomLevel(20)
                    .fillColor(DRAW_LINE_COLOR)
                    .fillAlpha(1.0f)
                    .fixed(false)
                    .randomOffset(false)
                    .build();
        }
        return defaultPointStyle;
    }

    public static Style getDefaultLineStyle() {
        if (defaultLineStyle == null) {
            defaultLineStyle = Style.builder()
                    .stippleColor(DRAW_LINE_COLOR)
                    .fillAlpha(1f)
                    .fillColor(DRAW_LINE_COLOR)
                    .stipple(DEFAULT_LINE_WIDTH)
                    .stippleWidth(DEFAULT_LINE_WIDTH)
                    .strokeWidth(DEFAULT_LINE_WIDTH)
                    .strokeColor(DRAW_LINE_COLOR)
                    .scaleZoomLevel(20)
                    .fixed(true)
                    .randomOffset(false)
                    .build();
        }
        return defaultLineStyle;
    }
    public static LineStyle getNaviLineStyle() {
        if (naviPathLineStyle == null) {
            naviPathLineStyle = LineStyle.builder()
                    .stippleColor(Color.RED)
                    .stipple(DEFAULT_LINE_WIDTH)
                    .stippleWidth(DEFAULT_LINE_WIDTH)
                    .color(Color.RED)
                    .strokeColor(Color.RED)
                    .strokeWidth(DEFAULT_LINE_WIDTH)
                    .randomOffset(false)
                    .build();
        }
        return naviPathLineStyle;
    }

    public static Style getDefaultPolygonStyle() {
        if (defaultPolygonStyle == null) {
            defaultPolygonStyle = Style.builder()
                    .stippleColor(DRAW_LINE_COLOR)
                    .stipple(DEFAULT_LINE_WIDTH)
                    .stippleWidth(DEFAULT_LINE_WIDTH)
                    .strokeWidth(DEFAULT_LINE_WIDTH)
                    .strokeColor(DRAW_LINE_COLOR)
                    .fillColor(DRAW_LINE_COLOR)
                    .fillAlpha(0.5f)
                    .scaleZoomLevel(20)
                    .fixed(true)
                    .randomOffset(false)
                    .build();
        }
        return defaultPolygonStyle;
    }

    public static MarkerSymbol getGeoJsonMarkerSymbol(Context mContext) {
        if (geoJsonMarkerSymbol == null) {
            Bitmap bitmapPoi = drawableToBitmap(mContext.getDrawable(R.drawable.geojson_point));
            geoJsonMarkerSymbol = new MarkerSymbol(bitmapPoi, MarkerSymbol.HotspotPlace.CENTER);
        }
        return geoJsonMarkerSymbol;
    }


    public static android.graphics.Bitmap makeTintBitmap(android.graphics.Bitmap inputBitmap, int tintColor) {
        if (inputBitmap == null) {
            return null;
        }

        android.graphics.Bitmap outputBitmap = android.graphics.Bitmap.createBitmap(inputBitmap.getWidth(), inputBitmap.getHeight(), inputBitmap.getConfig());
        Canvas canvas = new Canvas(outputBitmap);
        Paint paint = new Paint();
        paint.setColorFilter(new PorterDuffColorFilter(tintColor, PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(inputBitmap, 0, 0, paint);
        return outputBitmap;
    }
}
